﻿using System;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YJKPatternSign.StairsRoomPlanSign.ModelLayer;

namespace YJKPatternSign.StairsRoomPlanSign.ControlLayer
{
	public class SignManger
	{
		public static ExternalCommandData CommandData
		{
			get
			{
				return SignManger.m_commandData;
			}
			private set
			{
				SignManger.m_commandData = value;
			}
		}

		public static UIApplication Uiapp
		{
			get
			{
				return SignManger.m_uiapp;
			}
			private set
			{
				SignManger.m_uiapp = value;
			}
		}

		public static Application App
		{
			get
			{
				return SignManger.m_app;
			}
			private set
			{
				SignManger.m_app = value;
			}
		}

		public static UIDocument Uidoc
		{
			get
			{
				return SignManger.m_uidoc;
			}
			private set
			{
				SignManger.m_uidoc = value;
			}
		}

		public static Document Doc
		{
			get
			{
				return SignManger.m_doc;
			}
			private set
			{
				SignManger.m_doc = value;
			}
		}

		public static StairPlanDataPacker ThisStairPlandDataPacker
		{
			get
			{
				return SignManger.m_thisStairPlandDataPacker;
			}
			set
			{
				SignManger.m_thisStairPlandDataPacker = value;
			}
		}

		public SignManger(ExternalCommandData commandData)
		{
			SignManger.CommandData = commandData;
			SignManger.Uiapp = SignManger.CommandData.Application;
			SignManger.App = SignManger.Uiapp.Application;
			SignManger.Uidoc = SignManger.Uiapp.ActiveUIDocument;
			SignManger.Doc = SignManger.Uidoc.Document;
		}

		public bool TryGetStairPlanData()
		{
			bool result;
			try
			{
				SignManger.ThisStairPlandDataPacker = null;
				SignManger.ThisStairPlandDataPacker = this.GetDataPacker();
				result = (SignManger.ThisStairPlandDataPacker != null);
			}
			catch (Exception)
			{
				YJKMessageBox.Show("标注失败");
				SignManger.ThisStairPlandDataPacker = null;
				result = false;
			}
			return result;
		}

		public bool TryMakeSign()
		{
			if (SignManger.ThisStairPlandDataPacker == null)
			{
				return false;
			}
			try
			{
				this.m_useStairSignManger = new StairsSignManger(SignManger.ThisStairPlandDataPacker);
				this.m_useOutSideSignManger = new OutSideSignManger(SignManger.ThisStairPlandDataPacker);
				this.m_useOutSideSignManger.PrepareDimension();
				this.m_useStairSignManger.PrepareDimension();
			}
			catch (Exception)
			{
				return false;
			}
			TransactionGroup transactionGroup = new TransactionGroup(SignManger.Doc, "allSign");
			try
			{
				transactionGroup.Start();
				Transaction transaction = new Transaction(SignManger.Doc, "makeSign");
				try
				{
					transaction.Start();
					this.m_useOutSideSignManger.TryMakeSign(SignManger.Doc, SignManger.ThisStairPlandDataPacker.UseView);
					this.m_useStairSignManger.TryMakeSign(SignManger.Doc, SignManger.ThisStairPlandDataPacker.UseView);
					transaction.Commit();
				}
				catch (Exception)
				{
					if (transaction.HasStarted())
					{
						transaction.RollBack();
					}
					return false;
				}
				if (this.m_useMakeFillRegionLinker != null)
				{
					this.m_useMakeFillRegionLinker.MakeFillRegion();
				}
				transactionGroup.Assimilate();
			}
			catch (Exception)
			{
				if (transactionGroup.HasStarted())
				{
					transactionGroup.RollBack();
				}
				return false;
			}
			return true;
		}

		private StairPlanDataPacker GetDataPacker()
		{
			StairPlanDataPacker result;
			try
			{
				Reference reference = SignManger.m_uidoc.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new DetailViewBlockSelectFilter(), "选择一个详图索引框");
				result = new StairPlanDataPacker(SignManger.Doc, reference.ElementId);
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = null;
			}
			return result;
		}

		private static ExternalCommandData m_commandData;

		private static UIApplication m_uiapp;

		private static Application m_app;

		private static UIDocument m_uidoc;

		private static Document m_doc;

		private static StairPlanDataPacker m_thisStairPlandDataPacker;

		private StairsSignManger m_useStairSignManger;

		private OutSideSignManger m_useOutSideSignManger;

		private MakeFillRegionLinker m_useMakeFillRegionLinker = new MakeFillRegionLinker();
	}
}
